//
//  MYStatusPictureView.m
//  MYChatting
//
//  Created by mac on 16/8/22.
//  Copyright © 2016年 lhf. All rights reserved.
//

#import "MYStatusPictureView.h"
#import "Masonry.h"
#import "MYPictureViewCell.h"

CGFloat StatusPicturelMarigin = 12;
CGFloat PictureCellMargin = 5;

static NSString *resuseIdentifier = @"MYPictureViewCell";

@interface MYStatusPictureView ()<UICollectionViewDataSource>

@property (nonatomic,strong) NSArray *pic_strs;

@property (nonatomic,weak) UILabel *testLabel;//文案标签

@end

@implementation MYStatusPictureView



- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //flowLayout.itemSize = CGSizeMake(30, 30);
    flowLayout.minimumInteritemSpacing = PictureCellMargin;
    flowLayout.minimumLineSpacing = PictureCellMargin;
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        //设置数据源代理
        self.dataSource = self;
        
        //注册单元格
        [self registerClass:[MYPictureViewCell class] forCellWithReuseIdentifier:resuseIdentifier];
        
       [self setUpUi];
    }
    return self;
}


- (void)setUpUi{
    UILabel *testLabel = [[UILabel alloc] init];
    [self addSubview:testLabel];
    testLabel.textColor = [UIColor redColor];
    testLabel.font = [UIFont systemFontOfSize:36];
    self.testLabel = testLabel;
    
    [testLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
//    //本身尺寸约束的设置
//    CGSize pictureSize = [self setPictureViewSize];
//    [self mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(pictureSize);
//    }];
}


- (CGSize)setPictureViewSize{
    
    CGFloat imageCount = self.pic_strs.count;
    
    //设置layout的itemSize(默认的大小）
    CGFloat maxWidth = kScreenWidth - 2 *StatusPicturelMarigin;
    CGFloat itemWidth = (maxWidth - 2 * PictureCellMargin) / 3;
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth);
    
    //没有图片
    if (imageCount == 0) {
        return CGSizeZero;
    }else if(imageCount == 1){
       //一张图片
        CGSize imageSize = CGSizeMake(180, 120);
        //由于单张图片的itemSize和默认的大小不一样，所以重新赋值的
        flowLayout.itemSize = imageSize;
        return imageSize;
    }else if (imageCount == 4){
        //4张图片
        CGFloat w = itemWidth * 2 + PictureCellMargin;
        return CGSizeMake(w, w);
        
    }else{
        //其他2 3 5 6 7 8 9张图片
        CGFloat row = ((imageCount - 1) / 3 + 1);
        return CGSizeMake(maxWidth, row * itemWidth + (row - 1) * PictureCellMargin);
    }
    
    
}



- (void)setPic_urls:(NSArray *)pic_urls{
    _pic_urls = pic_urls;
    
   // [self reloadData];
   // NSLog(@"%@",pic_urls);
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSDictionary *picDict in pic_urls) {
        NSString *picString = picDict[@"thumbnail_pic"];
       // NSURL *pic_url = [NSURL URLWithString:picString];
        [arrM addObject:picString];
    }
    self.pic_strs = arrM;
   // NSLog(@"------%@",self.pic_strs);
    
    self.testLabel.text = [NSString stringWithFormat:@"%ld",self.pic_strs.count];
    
    
    
    
//    [self setUpUi];
    //本身尺寸约束的设置
    CGSize pictureSize = [self setPictureViewSize];
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(pictureSize);
    }];
    
    [self reloadData];
}


#pragma mark数据源方法的实现
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.pic_strs.count;
}

- (nonnull UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    MYPictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:resuseIdentifier forIndexPath:indexPath];
    NSString *pic_string = self.pic_strs[indexPath.row];
    cell.pic_string = pic_string;
    return cell;
}




@end
